<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    modelNum: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      seriesData: [],
      legendData: []
    }
  },
  watch: {
    modelNum: {
      deep: true,
      handler(val) {
        this.initChart(val)
      }
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHanlder = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHanlder)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHanlder)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(modelNum) {
      this.chart = echarts.init(this.$el, 'macarons')
      if (modelNum) {
            modelNum.nameArray.map(item => {
            this.legendData.push(item)
        })
            modelNum.basePieChartVOList.map(item => {
            this.seriesData.push({
               value: item.value,
               name: item.name
             })
        })
        //  this.seriesData = modelNum.basePieChartVOList
        //  this.legendData = modelNum.nameArray
      }
      console.log( this.seriesData)
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          // data: ['Industries', 'Technology', 'Forex', 'Gold', 'Forecasts']
          data: this.legendData
        },
        calculable: true,
        series: [
          {
            name: '数量',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            // data: [
            //   { value: 320, name: 'AC' },
            //   { value: 240, name: 'DC' },
            //   { value: 149, name: '引擎' },
            // ],
            data: this.seriesData,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
